package gui.menu;

import gui.CanvasManager;
import gui.screens.ListOfGamesScreen;

import java.io.IOException;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * Class for the start up menu.
 * @author Sandra Mayer
 *
 */
public class StartUpMenu extends Canvas implements CommandListener {

	private static final String FILENAME = "/mainWhite.jpg";
	private static final String CURSORNAME = "/arrowForMenu.png";

	private Image backgroundImg;
	private Image cursorImg;
	private int nrOfItems = 2;
	private int[][] posOfCursor = {{70, 83}, {70, 116}};
	
	private int pointerPos;
	private Command exitCmd;
	private Command okCmd;

	/**
	 * Constructor.
	 * @throws java.io.IOException IOException
	 */
	public StartUpMenu() throws java.io.IOException {
		CanvasManager.setCurScreen(this);
		backgroundImg = Image.createImage(FILENAME);
		cursorImg = Image.createImage(CURSORNAME);

		exitCmd = new Command("Exit", Command.EXIT, 1);
		okCmd = new Command("OK", Command.OK, 1);
		this.addCommand(exitCmd);
		this.addCommand(okCmd);
		this.setCommandListener(this);
	}

	/**
	 * Paints the menu.
	 */
	public void paint(Graphics g) {
		g.setColor(0xffffff);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.translate((getWidth() - backgroundImg.getWidth()) / 2,
				(getHeight() - backgroundImg.getHeight()) / 2);

		g.drawImage(backgroundImg, 0, 0, Graphics.TOP | Graphics.LEFT);

		g.drawImage(cursorImg, posOfCursor[pointerPos][0],
				posOfCursor[pointerPos][1], Graphics.HCENTER
						| Graphics.VCENTER);
		g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
				Font.SIZE_MEDIUM));
		g.setColor(0x000000);
		g.drawString("Join Game", 80, 75, Graphics.LEFT | Graphics.TOP);
		g.drawString("Settings", 80, 108, Graphics.LEFT | Graphics.TOP);
	}


	/**
	 * Checks the position of the pointer and does the action when fire was pressed.
	 */
	protected void keyPressed(int keyCode) {
		switch (getGameAction(keyCode)) {
		case UP:
			pointerPos = --pointerPos < 0 ? nrOfItems - 1 : pointerPos;
			break;
		case DOWN:
			pointerPos = ++pointerPos >= nrOfItems ? 0 : pointerPos;
			break;
		case FIRE:
			doDisplayAction();
			break;
		default:
			switch (keyCode) {
			case KEY_NUM2:
				pointerPos = --pointerPos < 0 ? nrOfItems - 1 : pointerPos;
				break;
			case KEY_NUM8:
				pointerPos = ++pointerPos >= nrOfItems ? 0 : pointerPos;
				break;
			case KEY_NUM5:
				doDisplayAction();
				break;
			}
		}
		repaint();
	}
	
	/**
	 * Checks the selected option and does the action for it.
	 *
	 */
	private void doDisplayAction() {
		if (pointerPos == 0) {
			try {
				CanvasManager.setCurrent(new ListOfGamesScreen());
			} catch (IOException e) {
				e.printStackTrace();
			}
		} else if (pointerPos == 1) {
			CanvasManager.setCurrent(SettingsMenu.getInstance());
		}
	}

	/**
	 * Does the command action.
	 */
	public void commandAction(Command cmd, Displayable arg1) {
		if (cmd == okCmd) {
			keyPressed(getKeyCode(FIRE));
		} else if (cmd == exitCmd) {
			CanvasManager.showExitScreen();
		}
	}	
}